package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarQuestionSelectBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class X3CarQuestionHorizontalAdapter extends RecyclerView.Adapter<X3CarQuestionHorizontalAdapter.CarQuestionHorizontalHolder> {

    private List<CarQuestionSelectBean> mList;
    private List<CarQuestionSelectBean> selectSingleList;
    private Context mContext;
    private String itemName;
    private String itemCode;


    public X3CarQuestionHorizontalAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();


    }
    public void setDataList(List<CarQuestionSelectBean> mList,List<CarQuestionSelectBean> selectSingleList,String itemName,String itemCode) {
        this.mList = mList;
        this.itemCode = itemCode;
        this.itemName = itemName;
        this.selectSingleList = selectSingleList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public X3CarQuestionHorizontalAdapter.CarQuestionHorizontalHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3CarQuestionHorizontalAdapter.CarQuestionHorizontalHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_crm_filter_car_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3CarQuestionHorizontalAdapter.CarQuestionHorizontalHolder holder, int position) {
        holder.tv_name.setText(mList.get(position).itemName+" "+mList.get(position).itemNames);

        holder.iv_img.setOnClickListener(v -> {
            if(mList.get(position).itemCode.equals(itemCode) && mList.get(position).itemCodes.equals("ALL")){//全选了
                selectSingleList.clear();
            }else {
                Iterator<CarQuestionSelectBean> iterator = selectSingleList.iterator();
                while (iterator.hasNext()) {
                    CarQuestionSelectBean next = iterator.next();
                    if (next == null) {
                        continue;
                    }
                    if (X3StringUtils.isEmpty(next.itemCodes)) {
                        continue;
                    }
                    if (next.itemName.equals(mList.get(position).itemName) && next.itemCodes.equals(mList.get(position).itemCodes)) {
                        iterator.remove();
                    }
                }
            }
            mList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position,mList.size() - position);
            if(carModleHorDeleteListener!=null){
                carModleHorDeleteListener.deleteHorModel();
            }

        });

    }

    CarModleHorDeleteListener carModleHorDeleteListener;

    public void setiOnDeleteHor(CarModleHorDeleteListener carModleHorDeleteListener) {
        this.carModleHorDeleteListener = carModleHorDeleteListener;
    }

    public interface CarModleHorDeleteListener {
        void deleteHorModel();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CarQuestionHorizontalHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        private ImageView iv_img;
        public CarQuestionHorizontalHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_img = itemView.findViewById(R.id.iv_img);

        }
    }
}
